import { defineStore } from "pinia";
import { getCardList, getChatInfo } from '@/api/index';
import { userInfo, messageInfo, card } from "@/model";
import { PersistedStateOptions } from "pinia-plugin-persistedstate";
interface state {
    cardList: Array<card>,
    detailList: card,
    userAttentList: Array<string>,
    chaterInfoList: Array<userInfo>,
    currentInfo: userInfo,
    userInfo: userInfo,
    messageList: Array<messageInfo>
}
export const userStore = defineStore('user', {
    state: (): state => {
        return {
            cardList: [],
            detailList: {} as card,
            chaterInfoList: [],
            currentInfo: {} as userInfo,
            userInfo: {} as userInfo,
            messageList: [],
            userAttentList: []
        }
    },
    getters: {
        unReadNum(): number {
            let num: number = 0
            this.messageList.forEach(item => {
                num += item.unReadNum
            })
            return num
        },
        personalCardList(): Array<card> {
            if (this.cardList.length == 0) {
                getCardList()
            }
            return this.cardList.filter((item) => {
                return item.publisherId == this.userInfo.id
            })
        },
        userCardList(): Array<card> {
            if (this.cardList.length == 0) {
                getCardList()
            }
            return this.cardList.filter((item) => {
                return item.publisherId == this.userInfo.id
            })
        },

    },
    actions: {
        findChater(id: string) {
            let index = this.messageList.findIndex(item => {
                return id == item.id;
            })

            return this.messageList[index];
        },
        setCardList() {
            if (this.cardList.length == 0) {
                getCardList().then((res: any) => {
                    console.log(789);
                    
                    this.cardList = res.data;
                })
            }
        },
        getDetail(id: string) {
            const result = this.cardList.find((item: card) => {
                return id == item.id;
            })
            //@ts-ignore
            this.detailList = result
        },
        getChaterInfo(id: string) {
            getChatInfo(id).then((res: any) => {
                if (!this.chaterInfoList.some(item => res.data.id == item.id)) {
                    this.chaterInfoList.push(res.data);

                    //@ts-ignore
                    this.currentInfo = res.data
                }
            })
        },
        addUnReadNum(id: string) {
            this.findChater(id).unReadNum++
        },
        addMessage(data: { message: string; senderId: string; receiverId: string; }, id: string) {
            this.findChater(id).messageCohort.push(data)
        },
        getNum(id: string): number {
            return this.findChater(id).unReadNum
        },
        resetNum(id: string) {
            this.findChater(id).unReadNum = 0
        },
        addMessageInfo(id: string) {
            this.messageList.push({
                id: id,
                messageCohort: [],
                unReadNum: 0
            })
        },
        getMessageList(id: string) {
            let index = this.messageList.findIndex(item => {
                return id == item.id;
            })
            if (index !== -1)
                return this.messageList[index].messageCohort
        },
        getLasterMessage(id: string) {
            let list = this.findChater(id).messageCohort
            if (list.length > 0) {
                return list[list.length - 1].message
            }
            else {
                return ''
            }

        }
    },
    persist: {
        enabled: true,	//开启
        storage: localStorage,	//修改存储位置
        key: 'userInfo',	//设置存储的key
        paths: ['userInfo', 'cardList', 'messageList', 'chaterInfoList'],//指定要长久化的字段
    } as PersistedStateOptions
})


